/**
 * Created by ybjaa on 2017/5/26.
 */

export class UploadFile{


    private _progressElement:HTMLElement=null;

    protected _prefix="uploadFile";

    protected _currentProgress:number=0;

    public State={
        success:false,
        error:false,
        isStartUpload:false
    };

    public ID:number;
    public FileName:string;

    public fileSize:number;




    get getID(){
        return this._prefix+this.ID;
    }

    get getFileInfo() {
        return this.FileName+"("+this.getSizeStr(this.fileSize)+")";
    }

    public set CurrentProgress(value:number){
        this._currentProgress=value;


        if(!this._progressElement)  this._progressElement=document.getElementById(this._prefix+this.ID);

        if(this._progressElement)
        {
            this._progressElement.style.width=this.CurrentProgress+"%";
            this._progressElement.children[0].innerHTML=this.CurrentProgress+"%";
        }

    }


    public get CurrentProgress():number{
        return Math.floor(this._currentProgress*10000)/100.0;
    }


    private getSizeStr(size:number):string {
        if (size < 0) return size + "";
        if (size < 1024) return size + "B";
        let m;
        m = size / 1024.0;
        if (m < 1024) return m.toFixed(2) + "KB";
        m = m / 1024.0;
        return m.toFixed(2) + "MB";

    }

    public setPrefix(value:string){
        this._prefix=value;
    }

    public initialize():void{

        this.State.success=false;
        this.State.error=false;
        this.State.isStartUpload=false;

        this.CurrentProgress=0;
    }





}